import {getTaskExamForms, getTaskExamlUserForms} from 'services/app';
import pathToRegexp from 'path-to-regexp/index';

function getNowFormatDate () {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    const currentdate = year + seperator1 + month;
    return currentdate;
}


export default {
    namespace: 'ExamForms',
    state: {
        pageIndex: 1,
        pageCount: 15,
        date: getNowFormatDate(),
        total: 0,
        modalVisible: false,
        records: [],
        userForms: {
            pageIndex: 1,
            pageCount: 10,
            date: getNowFormatDate(),
            userId: undefined,
            total: 0,
            records: [],
        },
    },
    effects: {
        *getForms ({ payload }, { call, put }) {
            const {pageIndex, pageCount, date} = payload;
            const rsp = yield call(getTaskExamForms, payload);
            const {total, data} = rsp;
            yield put({
                type: 'save',
                payload:{
                    pageIndex,
                    pageCount,
                    date,
                    records: data,
                    total,
                },
            });
        },
        *getUserForms ({ payload }, { call, put }) {
            const {pageIndex, pageCount, date, userId} = payload;
            const rsp = yield call(getTaskExamlUserForms, payload);
            const {total, data} = rsp;
            const userForms = {
                pageIndex,
                pageCount,
                date,
                userId,
                records: data,
                total,
            };
            yield put({
                type: 'save',
                payload:{
                    modalVisible: true,
                    userForms,
                },
            });
        },
        *closeModal ({ paylaod }, {call, put}) {
            yield put({
                type: 'save',
                payload:{
                    modalVisible: false,
                },
            });
        },
    },

    reducers: {
        save (state, { payload }) {
            return { ...state, ...payload };
        },
    },
};